//
//  AstroAppDelegate.m
//  Astro
//
//  Created by Trinh Huy Cuong on 11/27/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AstroAppDelegate.h"
#import "RootViewController.h"
#import "VCAbout.h"
#import "SOUser.h"
#import "VCLogin.h"
#import "VCAccount.h"
#import "VCAddDollar.h"
#import "VCDateInput.h"
#import "VCDataInput.h"

@implementation AstroAppDelegate

@synthesize window;
@synthesize navigationController, navigationItem;
@synthesize mainButtonItem;
@synthesize userInfo;
@synthesize dateInput, dataInput;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	
    // Set the navigation controller as the window's root view controller and display.
	//    self.window.rootViewController = self.navigationController;
	[self.window addSubview:self.navigationController.view];
	
	UIBarButtonItem *bt = [[UIBarButtonItem alloc] initWithTitle:@"Main" style:UIBarButtonItemStyleDone target:nil action:nil];
	self.navigationItem.rightBarButtonItem = bt;
	
	mainButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Main" style:UIBarButtonSystemItemBookmarks target:self action:@selector(mainButtonItemAction)];
		
	bottomButtonAccount = [UIButton buttonWithType:UIButtonTypeCustom];
	[bottomButtonAccount setFrame:CGRectMake(0, 460-45, 75, 75)];
	[bottomButtonAccount setContentMode:UIViewContentModeCenter];
	[bottomButtonAccount setTag:101];
	[bottomButtonAccount addTarget:self action:@selector(bottomButtonControlActions:) forControlEvents:UIControlEventTouchUpInside];
	[self.window addSubview:bottomButtonAccount];	
	
	bottomButtonAbout = [UIButton buttonWithType:UIButtonTypeCustom];
	[bottomButtonAbout setFrame:CGRectMake(320-75, 460-45, 75, 75)];
	[bottomButtonAbout setContentMode:UIViewContentModeCenter];	
	[bottomButtonAbout setTag:102];
	[bottomButtonAbout addTarget:self action:@selector(bottomButtonControlActions:) forControlEvents:UIControlEventTouchUpInside];
	[self.window addSubview:bottomButtonAbout];
	
	bottomButtonAddDolar = [UIButton buttonWithType:UIButtonTypeCustom];
	[bottomButtonAddDolar setFrame:CGRectMake(320-75, 460-45, 75, 75)];
	[bottomButtonAddDolar setContentMode:UIViewContentModeCenter];
	[bottomButtonAddDolar setTag:103];
	[bottomButtonAddDolar addTarget:self action:@selector(bottomButtonControlActions:) forControlEvents:UIControlEventTouchUpInside];	
	[self.window addSubview:bottomButtonAddDolar];	
	
	dateInput = [[VCDateInput alloc] init];
	[dateInput.view setFrame:CGRectMake(0, 460-252, 320, 252)];
	
	dataInput = [[VCDataInput alloc] init];
	[dataInput.view setFrame:CGRectMake(0, 460-252, 320, 252)];

    [self.window makeKeyAndVisible];
	
    return YES;
}

- (void) mainButtonItemAction{
	[self.navigationController popToRootViewControllerAnimated:YES];
}

- (void) bottomButtonControlWithState:(NSInteger)state andType:(NSInteger)type{
	if (type == BBTypeWest) {
		[bottomButtonAccount setImage:[UIImage imageNamed:@"Common.Profile.png"] forState:UIControlStateNormal];
		[bottomButtonAccount setImage:[UIImage imageNamed:@"Common.ProfileLight.png"] forState:UIControlStateHighlighted];

		[bottomButtonAbout setImage:[UIImage imageNamed:@"Common.About.png"] forState:UIControlStateNormal];
		[bottomButtonAbout setImage:[UIImage imageNamed:@"Common.AboutLight.png"] forState:UIControlStateHighlighted];

		[bottomButtonAddDolar setImage:[UIImage imageNamed:@"Common.Profile.png"] forState:UIControlStateNormal];
		[bottomButtonAddDolar setImage:[UIImage imageNamed:@"Common.ProfileLight.png"] forState:UIControlStateHighlighted];
	}
	else if(type == BBTypeEast){
		[bottomButtonAccount setImage:[UIImage imageNamed:@"Common.ProfileVang.png"] forState:UIControlStateNormal];
		[bottomButtonAccount setImage:[UIImage imageNamed:@"Common.ProfileVang.Light.png"] forState:UIControlStateHighlighted];
		
		[bottomButtonAbout setImage:[UIImage imageNamed:@"Common.AboutVang.png"] forState:UIControlStateNormal];
		[bottomButtonAbout setImage:[UIImage imageNamed:@"Common.AboutVang.Light.png"] forState:UIControlStateHighlighted];
		
		[bottomButtonAddDolar setImage:[UIImage imageNamed:@"Common.ProfileVang.png"] forState:UIControlStateNormal];
		[bottomButtonAddDolar setImage:[UIImage imageNamed:@"Common.ProfileVang.Light.png"] forState:UIControlStateHighlighted];		
	}
	
	switch (state) {
		case BBStateHide:{
			if (![bottomButtonAccount isHidden]) {
				[bottomButtonAccount setHidden:YES];
				[bottomButtonAbout setHidden:YES];
				[bottomButtonAddDolar setHidden:YES];
			}
			
			break;
		}
		case BBStateShowAbout:{
			if ([bottomButtonAbout isHidden]) {
				[bottomButtonAccount setHidden:NO];
				[bottomButtonAbout setHidden:NO];
			}
			break;
		}
		case BBStateShowDolar:{
			if ([bottomButtonAddDolar isHidden]) {
				[bottomButtonAccount setHidden:NO];
				[bottomButtonAddDolar setHidden:NO];
			}			
			break;
		}			
	}	
}

- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}

- (void) bottomButtonControlActions:(id)sender{
	NSLog(@"SenderTag: %d", [sender tag]);
	switch ([sender tag]) {
		case 101:{
			if (userInfo) {
				VCAccount *account = [[VCAccount alloc] initWithNibName:@"VCAccount" bundle:nil];
				[self.navigationController pushViewController:account animated:YES];
				[account release];
			}
			else {
				VCLogin *login = [[VCLogin alloc] initWithNibName:@"VCLogin" bundle:nil];
				[self.navigationController pushViewController:login animated:YES];
				[login release];				
			}

			break;
		}
		case 102:{
			VCAbout *about = [[VCAbout alloc] initWithNibName:@"VCAbout" bundle:nil];			
			[self.navigationController pushViewController:about animated:YES];
			[about release];					
			
			break;
		}
		case 103:{
			VCAddDollar *addDollar = [[VCAddDollar alloc] initWithNibName:@"VCAddDollar" bundle:nil];
			[self.navigationController pushViewController:addDollar animated:YES];
			[addDollar release];
			
			break;
		}
	}
}
	
#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
	[navigationController release];
	[window release];
	[super dealloc];
}


@end

